from car import Car
import time
import cv2
import sys
from datetime import datetime
from ColorDetect import get_center

if __name__ == "__main__":
    try:
        with Car(car_ip="127.0.0.1", car_port=8888, camera_id=0) as agv:
            print("开始捕获图像...")
            
            for i in range(100):
                # 获取当前时间
                
                
                # 正确解包get_frame()返回的元组
                ret, img = agv.get_frame()
                img,zb=get_center(img,2,True)
                # 检查是否成功获取图像
                if not ret or img is None:
                    print(f"无法获取图像，跳过第{i}帧")
                    continue
                call_time_str = datetime.now().strftime("%H:%M:%S.%f")[:-3]
                # 在图像上添加时间戳
                cv2.putText(img, f"Call: {call_time_str}", (10, 30),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
                
                # 保存图像
                filename = f'./pic/testpic{i:03d}.jpg'  # 使用3位数字格式化文件名
                cv2.imwrite(filename, img)
                print(f"已保存图像: {filename}")
                
            
            print("图像捕获完成")
            cv2.destroyAllWindows()
            
    except KeyboardInterrupt:
        print("程序被用户中断")
    except Exception as e:
        print(f"发生错误: {e}")
